package com.currency.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;

public class AppSecret {
    public static void main(String[] args) {
        String line = "{\\\"nonce_str\\\":\\\"OiFlAA6fj6PZuwzI\\\",\\\"appid\\\":\\\"wx910deb30bf41ba3a\\\",\\\"sign\\\":\\\"26F8EEFF66FCC4D6CA6AE83E407F3D60\\\",\\\"trade_type\\\":\\\"APP\\\",\\\"return_msg\\\":\\\"OK\\\",\\\"result_code\\\":\\\"SUCCESS\\\",\\\"mch_id\\\":\\\"1584805601\\\",\\\"return_code\\\":\\\"SUCCESS\\\",\\\"prepay_id\\\":\\\"wx16100339977728efdc0065e31680601600\\\"}";
        JSONObject jo = JSONObject.parseObject(line.replace("\\\"", "\""));
        System.out.println(jo.toJSONString());
    }

    public static JSONObject initWechatParam(String noncestr, String prepayid) {
        long time = System.currentTimeMillis() / 1000;
        System.out.println(time);

        StringBuffer allParam = new StringBuffer("");
        allParam.append("appid=wx910deb30bf41ba3a");
        allParam.append("&noncestr=" + noncestr);
        allParam.append("&package=WXPay");
        allParam.append("&partnerid=1584805601");
        allParam.append("&prepayid=" + prepayid);
        allParam.append("&timestamp=" + time);
        String sign = DigestUtils.md5Hex(allParam.toString() + "&key=b534811e05584cff8593237757dad7a2");
        sign = sign.toUpperCase();

//        StringBuffer xmlStr = new StringBuffer("");
//        xmlStr.append("<xml>");
//        xmlStr.append("<appid>wx910deb30bf41ba3a</appid>");
//        xmlStr.append("<noncestr>").append(noncestr).append("</noncestr>");
//        xmlStr.append("<partnerid>1584805601</partnerid>");
//        xmlStr.append("<package>WXPay</package>");
//        xmlStr.append("<prepayid>").append(prepayid).append("</prepayid>");
//        xmlStr.append("<timestamp>").append(time).append("</timestamp>");
//        xmlStr.append("<sign>").append(sign).append("</sign>");
//        xmlStr.append("</xml>");

        JSONObject jo = new JSONObject();
        jo.put("timestamp", time);
        jo.put("prepayid", prepayid);
        jo.put("noncestr", noncestr);
        jo.put("sign", sign);
        return jo;
    }
}
